package com.mco.service;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：liao.wei
 * @date ：2023/9/30 16:43
 * @package : com.mco.service
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class GuavaFilterService {
	
	// 定义常量
	public static final int _1W = 10000;
	// 定义Guava 布隆过滤器初始量
	public static final int SZIE = _1W * 100;
	//误判率 越小误判越少(为啥时0.03)
	public static final double FPP = 0.03;
	// 创建 Guava 布隆过滤器
	public static BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), SZIE);
	
	public void guavaFilter() {
		// 让guava加入100w白名单数据
		for (int i = 0; i < SZIE; i++) {
			bloomFilter.put(i);
		}
		//取10w不再合法范围内的数据，进行误判率演示
		List<Integer> list = new ArrayList<>(10 * _1W);
		//验证
		for (int i = SZIE + 1; i < SZIE + (10 * _1W); i++) {
			if (bloomFilter.mightContain(i)) {
				log.info("被误判了：{}",i);
				list.add(i);
			}
		}
		log.info("误判总量： {}",list.size());
	}
}
